package com.wwwarehouse.taskcenter.fragment.job_point.step7;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.JobPointRuleTemplateResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.wwwarehouse.taskcenter.fragment.job_point.AddJobPointCardFragment.KEY_BUSINESS_ID;

/**
 * Created by Jackie on 2017/11/13.
 * 设置作业点规则 选择添加方式 存放规则
 */

public class ChooseAddWayRuleTemplateFragment extends CommonBasePagerFragment {
    private String mBusinessId;

    private static final int REQUEST_GET_TEMPLATE_INFO = 0;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    private void initView() {
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mBusinessId = bundle.getString(KEY_BUSINESS_ID);
        }
    }

    private void initEvent() {

    }

    @Override
    public void onDownPull() {

    }

    @Override
    public void onRefreshListener() {
        getTemplateInfo();
    }

    @Override
    public void onLoadSuccess(String data, int what) {
        switch (what) {
            case REQUEST_GET_TEMPLATE_INFO:
                JobPointRuleTemplateResponseBean jobPointRuleTemplateResponseBean = JSON.parseObject(data, JobPointRuleTemplateResponseBean.class);
                ArrayList<JobPointRuleTemplateResponseBean.JobPointTemplateDetailsBean> jobPointTemplateDetailsList = (ArrayList<JobPointRuleTemplateResponseBean.JobPointTemplateDetailsBean>) jobPointRuleTemplateResponseBean.getList();

                try {
                    Bundle bundle = new Bundle();
                    bundle.putString(KEY_BUSINESS_ID, mBusinessId);
                    setData(jobPointRuleTemplateResponseBean.getTotal(), 8, new ChooseAddWayRuleTemplateDetailsFragment().getClass().getName(), bundle, jobPointTemplateDetailsList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onLoadError(String errorInfo) {
        toast(errorInfo);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseAddWayRuleTemplateFragment) {
            mActivity.setTitle(R.string.choose_add_rule_template);
        }
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

        getTemplateInfo();
    }

    /**
     * 查询模板
     */
    private void getTemplateInfo() {
        Map map  = new HashMap();

        map.put("page", 1);
        map.put("size", 8);

        loadData(TaskCenterConstant.URL_GET_TEMPLATE_INFO, map, REQUEST_GET_TEMPLATE_INFO);
    }
}
